/*
 *add:panels,panel高度宽度
 */
jQuery(function($){

// 设置 page-content的top值,使搜索条件与page内容不会重叠  page1.html
$( ".page-content").css("top",$( "#head-search").outerHeight() - 3 +"px");

// 收起页面顶部内容  index.html
var kit_header = $(".kit-header"),
    content_iframe = $( "#content-iframe"),
    kit_navbar = $( ".kit-navbar" );
$("#slideup-btn").bind({
    "mouseenter": function(){
        $( this).addClass("kit-state-hover");
    },
    "mouseleave": function(){
        $( this).removeClass("kit-state-hover");
    },
    "click": function(){
        if( kit_header.css("top") == "-57px" ){
            $(this).removeClass("kit-state-clicked");
            $(".kit-header").animate({top:"0"});
            content_iframe.animate({top:"89px"});
            kit_navbar.animate({top:"0"});
        }else{
            $(this).addClass("kit-state-clicked");
            $(".kit-header").animate({top:"-57px"});
            content_iframe.animate({top:"29px"});
            kit_navbar.animate({top:"-57px"});
        }
    }
});


// 鼠标移进移出表格状态
$(document).on({
    "mouseenter": function(){
        $(this).addClass("ui-states-hover");
    },
    "mouseleave": function(){
        $(this).removeClass("ui-states-hover");
    }
},".btn-more");

$(document).on({
//    "mouseenter": function(event){
//        var currentTarget = $(event.currentTarget);
//        currentTarget.addClass("tr-state-hover");
//    },
//    "mouseleave": function(event){
//        var currentTarget = $(event.currentTarget);
//        currentTarget.removeClass("tr-state-hover");
//    },
    "click": function(e){
        $(this).siblings().removeClass("tr-state-select");
        if($(this).hasClass("tr-state-select")){
            //$(this).removeClass("tr-state-select");
        }else{
            if(!$(this).hasClass("tr-state-disable")){
                $(this).addClass("tr-state-select");
            }
        }
    }
},".panel-table tbody tr:not('.tableDate,.sub-table')");




// 顶部模块选择框
//$("#header-select").click(function(event){
//    var targetObj = $(event.target),
//        options = $(this).find(".options"),
//        input = $(this).find("input"),
//        mask = $(this).siblings(".mask");
//    if(targetObj.is("button")||targetObj.is("span.border-left")){
//        if(options.is(":visible")){
//            options.hide();
//            mask.remove();
//        }else{
//            options.show();
//            if(!mask.size()){
//                if(document.attachEvent){
//                    var height = 23; // ie
//                }else {
//                    var height = 16; // 非ie
//                }
//                $("<div>").addClass("mask").css({
//                    height:options.outerHeight() +height+"px",
//                    top:"23",
//                    right:"-3px",
//                    left:"3px",
//                }).insertAfter($(this))
//            }
//        }
//    }
//
//    if(targetObj.is("a")&&targetObj.parent().hasClass("options")){
//        targetObj.siblings(".kit-state-clicked").removeClass("kit-state-clicked");
//        targetObj.addClass("kit-state-clicked");
//        input.val(targetObj.text());
//        options.hide();
//        mask.remove();
//    }
//});
var selectOptions = $("#header-select").find(".options");
$(document).on("click",function(event){
    event.stopPropagation();
    var target = $(event.target);
    if(!target.is("#header-select")&&!target.parents("#header-select").size()){
        selectOptions.hide();
        $("#header-select").siblings(".mask").remove();
    }
})

// panel-head 搜索框
$(".js-kit-searchTips").click(function(){
//    $(".searchMask").remove();
    if($(this).data("target")){
        var searchTips = $("#"+$(this).data("target")),
            searchMask = searchTips.siblings(".searchMask"),
            searchArrow = searchTips.find(".searchArrow");
        var left = $(this).offset().left;

        if(!searchMask.size()){
            searchMask = $("<div>").addClass("searchMask").insertAfter(searchTips);
        }

        if(searchTips.is(":visible")){
            searchTips.hide();
            searchMask.hide();
        }else{
            $(".searchTips").hide();
            searchTips.show();
            // searchTips.find("input").width(searchTips.width()-36)
            searchArrow.offset({left:left+5});
            searchMask.css({
                height:searchTips.outerHeight()+"px"
            }).show();
        }
        searchTips.find("button").click(function(){
            searchTips.hide().siblings(".searchMask").hide();
        });
        ;(function( searchTips, searchMask, $, undefinded ){
            var timer = null,
                area = { x1: 0, x2: 0, y1: 0, y2: 0 };

            timer = setTimeout(  function(){
                    searchTips.hide();
                    searchMask.hide();
            }, 3000 );

            searchTips.mouseenter(function(){
                $( document ).on( "mousemove", function(e){
                    if( timer ){
                        clearTimeout( timer );
                    }
                    timer = setTimeout(  function(){

                        var _x = e.clientX,
                            _y = e.clientY;

                        area.x1 = searchTips.offset().left;
                        area.y1 = searchTips.offset().top;
                        area.x2 = area.x1 + searchTips.outerWidth();
                        area.y2 = area.y1 + searchTips.outerHeight();

                        if( _x < area.x1 || _x > area.x2 || _y < area.y1 || _y > area.y2 ){
                            searchTips.hide();
                            searchMask.hide();
                            $( document).off("mousemove");
                        }

                    }, 100 );
                } );
            });

        })( searchTips, searchMask, jQuery );
    }
});

// 展开隐藏左侧树  content.html
$("#js-hideTree").bind({
    "mouseenter": function(){
        $(this).addClass("kit-state-hover");
    },
    "mouseleave": function(){
        $(this).removeClass("kit-state-hover");
    },
    "click": function(){
        $("#content-right").css("left",0);
        $(".page-content",window.frames[0].document).css("margin-left","-10px")
        $("#content-left").css({
            position:"absolute",
            left:0,
            zIndex:100
        }).animate({
                left:"-"+$("#content-left").outerWidth()+"px",
                opacity:0
        },function(){
                $("#js-kit-showTree").show();
                if($("#js-kit-showTree").offset().left < 0){
                    $("#js-kit-showTree").offset({left:0});
                };
        });
    }
})
$("#js-kit-showTree").click(function(){
    $(this).hide();
    $(".page-content",window.frames[0].document).css("margin-left","0")
    $("#content-right").css("left","219px");
    $("#content-left").animate({
            left:"0px",
            opacity:1
        },function(){
            $(this).css({
                position:"relative"
            });
        }).show();
})


// 查询显示没有数据，查看原因
$(document).on({
    "click":function(){
        var nodataBox = $(this).parents(".nodataBox"),
            reason = nodataBox.find(".reason"),
            reasonWrapper = nodataBox.find(".reasonWrapper"),
            button = null,
            nodataBox_h = nodataBox.outerHeight(),
            reasonWrapper_h,
            reason_h;

        if(!reasonWrapper.size()){
            reasonWrapper = $("<div>").addClass("reasonWrapper").appendTo(nodataBox);
            reason.appendTo(reasonWrapper);
            $("<button>").text("返回").addClass("kit-button").appendTo(reasonWrapper).button({
                icons: {
                    primary: "kit-icon-back kit-icon"
                }
            })
            .removeClass("ui-corner-all")
            .click(function(){
                    reasonWrapper.hide()
                        .siblings(".nodata").show();
                    nodataBox.css({"background":"none","z-index":1});
            });
            reasonWrapper.find("button .ui-button-text").append("<span class='button-left'></span><span class='button-right'></span>")
            // 设置reson的高度,去掉最后table-row的底边框
            reasonWrapper_h = reasonWrapper.outerHeight(),
            reason_h = reason.outerHeight();
            reason_h = reason_h > (reasonWrapper_h-40) ? (reasonWrapper_h-40) : reason_h;
            reason.height(reason_h);
            reason.find(".table-row:last").css("border","0");
            // 设置reasonWrapper垂直居中
            if(reasonWrapper_h < nodataBox_h){
                reasonWrapper.css("margin-top",(nodataBox_h - reason_h - 40)/2+"px");
            }
        }
        nodataBox.css({"background":"#fff","z-index":100}).find(".nodata").hide();
        reasonWrapper.show();

    }
},".js-reason-btn")

// 多行查询条件 收缩样式
var searchContainer = $("#head-search").find(".search-container");
if(searchContainer.size()&&$("#search-toggle").size()){
    var rows = searchContainer.find(".row"),
        toggleBtn = $("#search-toggle");

    rows.each(function(){
        if($(this).index()==0){
            $(this).addClass("bottomborder");
        }
        if($(this).index()==0 || $(this).is(":last-child")){
            $(this).show();
        }else{
            $(this).hide();
        }
    });
    $( ".page-content").css("top",$( "#head-search").outerHeight() - 3 +"px");
    toggleBtn.on("click",function(){
        if($(this).hasClass("searchOpen")){
            rows.each(function(){
                if($(this).index()==0){
                    $(this).addClass("bottomborder");
                }
                if($(this).index()==0  || $(this).is(":last-child")){
                    $(this).show();
                }else{
                    $(this).hide();
                }
            });
            $(this).html("点击展开<i class='kit-icon kit-icon-arrowdown3'></i>");
            $( ".page-content").css("top",$( "#head-search").outerHeight() - 3 +"px");
        }else{
            rows.each(function(){
                $(this).show().removeClass("bottomborder");
            });
            $(this).html("点击收起<i class='kit-icon kit-icon-arrowdown4'></i>");
            $( ".page-content").css("top",$( "#head-search").outerHeight() - 3 +"px");
        }
        rows.filter(":visible").each(function(i){
            if(i == (rows.filter(":visible").size()-1) ){
                $(this).addClass("bottomborder")
            }
        })
        $(this).toggleClass("searchOpen");

    })
}

/* tablerow表格内容被下面的遮挡bug
 * date: 2014-08-26
 * -----------------------------------------------------------------------
 */
$( ".table-row" ).on( {
    "mouseenter": function(){
     //   $( this).css( "z-index", 100).siblings().css( "z-index", 1 );
    }
} )

});

function formateNum(num){
	return parseInt(num,10);	
}

function resetGrid(id){ // 重置jqGrid表格样式
    var table = $("#"+id);
        table.jqGrid("setGridWidth",table.parents(".panel-table").width()); // 设置表格宽度
        table.parents(".ui-jqgrid").removeClass("ui-corner-all"); // 移除圆角
        table.find("tr:even").addClass("odd").end().find("tr:odd").addClass("even"); // tr奇偶变色
}

/* 删除数据不可撤消操作
 * -----------------------------------------------------------------------
 */
function breakInfo(options){
    var defaults = {
        title:"操作标题!",
        details:"操作详情",
        buttons:[
            {text:"确定",
             addClass:"js-kit-icon-ok",
             click:function(){
                  destory();
                }
            },
            {text:"取消",
             addClass:"js-kit-icon-cancel",
             click:function(){
                  destory();
                }
            }
        ],
        width:400
    }
    var options = $.extend({},defaults,options);
    var breakInfo = $("<div />").appendTo($("body"));
    $("<div class='details'>"+options.details+"</div>").appendTo(breakInfo);
    breakInfo.dialog({
        title: options.title,
        draggable: false,
        modal: true,
        resizable: false,
        width:options.width,
        dialogClass:"breakInfo",
        position:{
            my:"center center",
            at:"center center-60",
            of:"body"
        },
        buttons:options.buttons,
        create: function(){
                $(this).dialog("widget").find("button").each(function(){
                    if($(this).hasClass("js-kit-icon-ok")){
                        var iconClass = "kit-icon-ok";
                        var state = "warn"
                    }
                    if($(this).hasClass("js-kit-icon-cancel")){
                        var iconClass = "kit-icon-cancel";
                        var state = "normal"
                    }
                    $(this).setButton({
                        iconClass: iconClass,
                        state:state
                    }).append("<span class='button-left'></span><span class='button-right'></span>");
                });
               $("<span class='left'><i class='top'></i><i class='middle'></i> <i class='bottom'></i> </span><span class='right'><i class='top'></i><i class='middle'></i><i class='bottom'></i></span>").appendTo( $(this).dialog("widget") )
        }
    })
    $.destoryInfo = function destory(){
        breakInfo.dialog("destroy");
        breakInfo.remove();
    }
}

/* 加载子表格
 * date: 2014-07-17
 * -----------------------------------------------------------------------
 */

$.fn.loadSubtable = function(opts){
    var defaults = {
            colspan:6,
            load: $.noop
        },
        options = $.extend({},defaults,opts);
    return this.each(function(){
        var table = $(this),
            trs = table.find("tbody tr"),
            btns = trs.find(".btn-more");

        // 修正IE下展开详情改变表格布局  表格为固定布局方式
        table.css("table-layout","fixed");

        trs.on("click",function(e){
            e.stopPropagation();

            $(this).siblings().removeClass("tr-state-select");
            if(!$(this).hasClass("tr-state-select")){
                if(!$(this).hasClass("tr-state-disable")){
                    $(this).addClass("tr-state-select");
                }
            }

            var btn = $(this).find(">td >.btn-more"),
                next_tr = $(this).next(),
                subtableWrapper = "",
                subtable;

            if(btn.size()){
                if(!next_tr.is(".sub-table")){
                    subtable = $("<tr class='sub-table'><td colspan='"+options.colspan+"'><div class='table-container'></div></td></tr>").insertAfter($(this));
                    subtableWrapper = subtable.find(".table-container");
                    options.load(subtableWrapper,$(this));
                }
                toggleSubTable($(this));
            }
        })
    });
}
function toggleSubTable(tr){
    var next_tr = tr.next(),
        btn = tr.find(".btn-more");

    // 切换sub-table 的显示隐藏
    if(next_tr.size()){
        if(next_tr.is(".sub-table")){
            next_tr.siblings(".sub-table").hide();
            next_tr.toggle();
        }else{
            tr.siblings(".sub-table").hide();
        }
    }else{
        tr.siblings(".sub-table").hide();
    }

    // 切换按钮的状态
    if(btn.hasClass("btn-close-more")){
        btn.text("展开").removeClass("btn-close-more");
    }else{
        tr.siblings().find(".btn-close-more").text("展开").removeClass("btn-close-more");
        btn.text("收起").addClass("btn-close-more");
    }
}
